package com.insectstudio.haiivloffical.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.insectstudio.haiivloffical.CommentsActivity;
import com.insectstudio.haiivloffical.Constants;
import com.insectstudio.haiivloffical.MyRatioDrawable;
import com.insectstudio.haiivloffical.R;
import com.insectstudio.haiivloffical.ViewImageActivity;
import com.insectstudio.haiivloffical.model.HaiivlItem;

public class haiivlAdapter extends ArrayAdapter<HaiivlItem> {
	private Context context;
	private int resource;
	private ArrayList<HaiivlItem> objects;

	private AQuery listAq ; 
	public haiivlAdapter(Context context, int resource,
			ArrayList<HaiivlItem> objects , AQuery listAq) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.objects = objects;
		this.listAq = listAq;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {		
		// TODO Auto-generated method stub
		View view = convertView;
		ViewHolder holder = null;
		if (null == view) {
			LayoutInflater inflate = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflate.inflate(resource, parent, false);
			holder = new ViewHolder();
			holder.txtTitle = (TextView) view.findViewById(R.id.tvTitle);
			holder.txtUploader = (TextView) view.findViewById(R.id.tvUploader);
			holder.txtComments = (TextView) view.findViewById(R.id.tvComment);
			holder.txtNumLike = (TextView) view.findViewById(R.id.tvNumLike);
			holder.imgPhoto = (ImageView) view.findViewById(R.id.imgView);
			holder.btnYoutube = (ImageView) view.findViewById(R.id.btn_youtube);
			holder.btnViewImage = (Button) view.findViewById(R.id.btn_view_detail);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		HaiivlItem item = getItem(position);
		final AQuery aq = listAq.recycle(view);
		// set data
		aq.id(holder.txtTitle).text(item.getTitle());
		aq.id(holder.txtUploader).text("Đăng bởi: " + item.getFullName());
		aq.id(holder.txtComments).text(String.valueOf(item.getComments()));
		aq.id(holder.txtNumLike).text(String.valueOf(item.getLiked()));
		// set click listener
		 String imageUrl = item.getImageUrl();
		 try {
			 final ViewHolder temp = holder;
			 aq.id(holder.imgPhoto).image(imageUrl, true, true, 0, R.id.imgView, new BitmapAjaxCallback() {
				 @Override
				protected void callback(String url, ImageView iv, Bitmap bm,
						AjaxStatus status) {
					// TODO Auto-generated method stub
					super.callback(url, iv, bm, status);
					if (bm.getHeight() > 3000) {
						Bitmap scale = Bitmap.createBitmap(bm, 0, 0, bm.getWidth() , bm.getWidth());
						iv.setImageDrawable(new MyRatioDrawable(context.getResources(), scale, iv, AQuery.RATIO_PRESERVE, 0.0f));
						aq.id(temp.btnViewImage).visibility(View.VISIBLE);
					} else {
						iv.setImageDrawable(new MyRatioDrawable(context.getResources(), bm, iv, AQuery.RATIO_PRESERVE, 0.0f));
						aq.id(temp.btnViewImage).visibility(View.GONE);
					}
				}
			 });			 
		 } catch(Exception e) {
			 e.printStackTrace();
		 }
		 final String imageViewedUrl = imageUrl;
		 final String title = item.getTitle();
		 aq.id(holder.btnViewImage).clicked(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(context, ViewImageActivity.class);
				i.putExtra("imageUrl", imageViewedUrl);
				i.putExtra("title", title);
				context.startActivity(i);
				
			}
		});
		 String type = "photo";
		 if (item.getVideo_id().equals("")) {
			 aq.id(holder.btnYoutube).gone();
		 } else {
			 type = "video";
			 aq.id(holder.btnYoutube).visible();
			 final String videoId = item.getVideo_id();
			 aq.id(holder.btnYoutube).clicked(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					getContext().startActivity(new Intent(Intent.ACTION_VIEW, 
							Uri.parse(Constants.YOUTUBE_URL + videoId)));
				}
			});
		 }
		 final int photoId = getItem(position).getPhotoId();
		 final String urlType = type;
		 aq.id(holder.imgPhoto).clicked(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(context, CommentsActivity.class);
				i.putExtra("commentsUrl", Constants.FB_COMMENTS_URL.replace("[type]", urlType).replace("[photoId]", String.valueOf(photoId)));
				context.startActivity(i);
				
			}
		});
		 
		return view;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return objects.size();
	}
	
	@Override
	public HaiivlItem getItem(int position) {
		// TODO Auto-generated method stub
		return objects.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	static class ViewHolder {
		TextView txtTitle, txtUploader, txtNumLike, txtComments;
		ImageView imgPhoto, btnYoutube;
		Button btnViewImage;
	}
}
